<?php

class BaseTask {
	
	protected $job;
	protected $task;
	protected $taskService;
    protected $jobService;
    protected $schedulerService;
    protected $resourceService;
	
	public function __construct($task) {
		$this->task = $task;
		$this->taskService = TaskService::getInstance();
        $this->jobService = JobService::getInstance();
        $this->schedulerService = SchedulerService::getInstance();
        $this->resourceService = ResourceService::getInstance();
        $this->job = $this->jobService->getJobById($this->task['job_id']);
	}
	
	public function selectExecutor() {
		Yii::log('select executor machine for task:'. $this->task['task_id'], CLogger::LEVEL_INFO, SCHEDULER_LOG);
			
		if(($executorMachine = $this->schedulerService->selectExecutorMachine()) === false) {
			Yii::log('select executor machine failed for task:'. $this->task['task_id'], 
				CLogger::LEVEL_ERROR, 
				SCHEDULER_LOG);
				
			$this->taskService->editTaskAttributes($this->task['task_id'], 
				array(
					'state'		=>	TaskState::Failed, 
					'detail'	=>	'select executor machine failed',
				));
			return false;
		}
				
		Yii::log('select executor machine succeessfully for task: '. $this->task['task_id'], CLogger::LEVEL_INFO, SCHEDULER_LOG);
		
		$this->taskService->editTaskAttributes($this->task['task_id'], 
			array(
				'executor_id'	=>	$executorMachine['executor_id'], 
				'state'		=>	TaskState::Ready,
				'detail'	=>	'',
			));
			
		Yii::log("task [{$this->task['task_id']}] in ready state", CLogger::LEVEL_INFO, SCHEDULER_LOG);	
		return true;		
	}
	
	/*
	protected function createTaskInfo() {
		Yii::log('create task info for task: '. $this->task['task_id'], CLogger::LEVEL_INFO, SCHEDULER_LOG);

        $this->job = $this->jobService->getJobById($this->task['job_id']);
        if(!isset($this->job)) {
        	Yii::log('can not find corresponding job for task: '. $this->task['task_id'], CLogger::LEVEL_ERROR, SCHEDULER_LOG);
            return false;
        }   
          
		$inputs = $outputs = array();  
		if(false === $this->createInputs($this->job['depends'], $inputs)) {
			return false;
		}
		if(false === $this->createOutputs($this->job['outputs'], $outputs)) {
			return false;
		}
		
        $taskInfo = array(
            'inputs'	=>	$inputs,
            'outputs'	=>	$outputs,
            'params'	=>	$this->job['job_param'],
        	'workflow'	=>	$this->job['workflow_id'],
        );
        $this->task['runtime'] = $taskInfo; 
        return true;
	}
	
	protected function createInputs($jobDepends, &$inputs) {
		foreach ($jobDepends as $depend) {
            $resource = $this->resourceService->getResourceById($depend['res_id']);
            if(!isset($resource)) {
            	Yii::log("job input resource[{$depend['res_id']}] can't find", CLogger::LEVEL_ERROR, SCHEDULER_LOG);
            	return false;
            }            
            $physicalResource = $this->resourceService->getResourcePhysicalByType($resource['res_id'], PhysicalType::Hive);
			if(!isset($physicalResource)) {
            	Yii::log("job input resource[{$resource['res_name']}] can't find hive physical", CLogger::LEVEL_ERROR, SCHEDULER_LOG);
            	return false;
            }
            
            $inputs[] = array(
            	'res_id'	=>	$resource['res_id'],
            	'res_name'	=>	$resource['res_name'],
            	'res_type'	=>	$resource['res_type'],
            	'res_frequency'		=>	$resource['res_frequency'],
            	'res_identifier'	=>	$resource['res_identifier'],
            	'res_fp'	=>	$resource['res_fp'],
            	'physical'	=>	$physicalResource,
            	'depend'	=>	$depend['depend'],
            );
        }
        return true;
	}
	
	protected function createOutputs($jobOutputs, &$outputs) {
		foreach ($jobOutputs as $output) {
	 		$resource = $this->resourceService->getResourceById($output['res_id']);
            if(!isset($resource)) {
            	Yii::log("job output resource[{$output['res_id']}] can't find", CLogger::LEVEL_ERROR, SCHEDULER_LOG);
            	return false;
            }            
            $physicalResource = $this->resourceService->getResourcePhysicalByType($resource['res_id'], PhysicalType::Hive);
			if(!isset($physicalResource)) {
            	Yii::log("job output resource[{$resource['res_name']}] can't find hive physical", CLogger::LEVEL_ERROR, SCHEDULER_LOG);
            	return false;
            }
            
            $outputs[] = array(
            	'res_id'	=>	$resource['res_id'],
            	'res_name'	=>	$resource['res_name'],
            	'res_type'	=>	$resource['res_type'],
            	'res_frequency'		=>	$resource['res_frequency'],
            	'res_identifier'	=>	$resource['res_identifier'],
            	'res_fp'	=>	$resource['res_fp'],
            	'physical'	=>	$physicalResource,
            	'export'	=>	$output['export'],
            );
        }
        return true;
	}
	*/

}
